package cn.smp.server.handler;

import cn.smp.message.ChatRequestMessage;
import cn.smp.message.ChatResponseMessage;
import cn.smp.server.session.SessionFactory;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

@ChannelHandler.Sharable
public class ChatRequestMessageHandler extends SimpleChannelInboundHandler<ChatRequestMessage> {
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, ChatRequestMessage msg) throws Exception {
        final String to = msg.getTo();
        final Channel channel = SessionFactory.getSession().getChannel(to);
        if (channel == null) {
            ctx.writeAndFlush(new ChatResponseMessage(false,"对方用户不存在或者不在线"));
            return;
        }
        channel.writeAndFlush(new ChatResponseMessage(msg.getFrom(),msg.getContent()));
    }
}
